package com.shanghe.house.controller;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.shanghe.house.common.Result;
import com.shanghe.house.dto.TimerDTO;
import com.shanghe.house.entity.Timer;
import com.shanghe.house.service.TimerService;

import jakarta.validation.Valid;

/**
 * 定时任务控制器
 * 处理定时任务相关的HTTP请求，提供RESTful API接口
 */
@RestController
@RequestMapping("/api/timers")
public class TimerController {
    private final TimerService timerService;

    public TimerController(TimerService timerService) {
        this.timerService = timerService;
    }

    @GetMapping
    public Result<List<Timer>> findAll() {
        return Result.success(timerService.findAll());
    }

    @GetMapping("/{id}")
    public Result<Timer> findById(@PathVariable Long id) {
        return Result.success(timerService.findById(id));
    }

    @PostMapping
    public Result<Timer> create(@Valid @RequestBody TimerDTO timerDTO) {
        return Result.success(timerService.create(timerDTO));
    }

    @PutMapping("/{id}")
    public Result<Timer> update(@PathVariable Long id, @Valid @RequestBody TimerDTO timerDTO) {
        return Result.success(timerService.update(id, timerDTO));
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        timerService.delete(id);
        return Result.success(null);
    }

    @PutMapping("/{id}/toggle")
    public Result<Timer> toggleStatus(@PathVariable Long id) {
        return Result.success(timerService.toggleStatus(id));
    }
} 